package com.csu.mall.product.app;

import com.csu.mall.common.utils.PageUtils;
import com.csu.mall.common.utils.R;
import com.csu.mall.product.entity.AttrEntity;
import com.csu.mall.product.entity.AttrGroupEntity;
import com.csu.mall.product.service.AttrGroupService;
import com.csu.mall.product.vo.AttrGroupVo;
import com.csu.mall.product.vo.AttrVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author zhenhuanli
 * @email 743879362@qq.com
 * @date 2022-09-16 11:42:54
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
//		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupEntity attrGroup = attrGroupService.getAttrGroup(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @GetMapping("/list/{catelogId}")
    public R getAttrGroups(@PathVariable("catelogId") long catelogId, @RequestParam Map<String, Object> params){
        //TODO 参数校验
        return R.ok().put("page", attrGroupService.queryPage(catelogId, params));
    }
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrsOfAttrGroup(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities = attrGroupService.getAttrsOfAttrGroup(attrgroupId);
        return R.ok().put("data", attrEntities);
    }
    @PostMapping("attr/relation/delete")
    public R deleteAttr(@RequestBody List<AttrVo> attrVos){
        attrGroupService.deleteAttrsOfAttrGroup(attrVos);
        return R.ok();
    }

    /**
     *  查找属性分组能够关联的属性
     *  要求
     *  1. 这些属性是属性分组所属分类下的属性，
     *  2. 这些属性没有被其他分组关联
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R selectEnableAttr(@PathVariable("attrgroupId") Long attrgroupId){

        PageUtils pageUtils = attrGroupService.selectEnableAttr(attrgroupId);

        return R.ok().put("page", pageUtils);
    }
///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R saveAttrAttrGroupRelation(@RequestBody List<AttrVo> attrVos){
        attrGroupService.saveBatchAttrAttrGroup(attrVos);
        return R.ok();
    }
// /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupVo> list = attrGroupService.getAttrGroupAndAttr(catelogId);
        return R.ok().put("data", list);
    }
}
